KindEditor.plugin('math', function (K) {
    var self = this, name = 'math', lang = self.lang(name + '.');
    self.clickToolbar(name, function () {
        var img = self.plugin.getSelectedImage();
        var latex = $(img).data("latex") || '';
        var html = [
            '<div class="ke-dialog-content-inner">',
            '<div class="tabs"></div>',
            '<div class="ke-formula" style="width:510px;height:380px;"></div>',
            '</div>'
        ].join('');
        var iframe = K('<iframe class="" frameborder="0" src="' + self.pluginsPath + 'math/formula.html?latex=' + encodeURIComponent(latex) + '&previewUrl=' + encodeURIComponent(self.options.formulaPreviewUrl) + '" style="width:100%;height:300px;"></iframe>');

        var dialog = self.createDialog({
                name: name,
                width: Math.min(document.body.clientWidth, 500),
                height: 380,
                title: "插入公式",
                body: html,
                yesBtn: {
                    name: '插入',
                    click: function (e) {
                        var win = iframe[0].contentWindow;

                        var currentSVG = win.$("#preview-body")[0].querySelector("svg");
                        if (!currentSVG) {
                            alert("请输入正确的公式");
                        }

                        const blob = new Blob([currentSVG.outerHTML], { type: 'image/svg+xml' });
                        const file = new File([blob], "latex.svg", { type: 'image/svg+xml' });

                        require(['upload'], function(Upload){
                            Upload.api.send(file, function (data, ret) {
                                var url = Fast.api.cdnurl(ret.data.url);
                                var latex = win.$("#latex-source").val();
                                if (latex == '') {
                                    Layer.msg("请选择或输入公式");
                                    return false;
                                }
                                self.insertHtml("<img src='" + url + "' data-latex='" + latex + "'>");
                                self.hideDialog().focus();
                            });
                        });
                        return;
                    }
                },
                noBtn: {
                    name: self.lang('no'),
                    click: function (e) {
                        self.hideDialog().focus();
                    }
                }
            }),
            div = dialog.div;

        K('.ke-formula', div).replaceWith(iframe);
        return;

    });
});
